<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring MVC RESTful API 完全指南</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 8px 16px;
            font-size: 12px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        
        pre {
            margin: 0;
            padding: 16px;
            overflow-x: auto;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 2px;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 1.5rem;
            border-radius: 8px;
            margin: 1.5rem 0;
        }
        
        .status-code {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-weight: 600;
            font-size: 14px;
            margin-right: 8px;
        }
        
        .status-success {
            background: #d1fae5;
            color: #065f46;
        }
        
        .status-error {
            background: #fee2e2;
            color: #991b1b;
        }
        
        .status-info {
            background: #dbeafe;
            color: #1e40af;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                Spring MVC RESTful API
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                构建现代化、可扩展的 Web 服务完全指南
            </p>
            <div class="flex justify-center space-x-6 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-code mr-2"></i>
                    <span>优雅的代码</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-rocket mr-2"></i>
                    <span>高性能架构</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>最佳实践</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Spring MVC 中实现 RESTful API 是一种常见的需求，它允许你构建符合 REST（Representational State Transfer）原则的 API，这种 API 具有良好的可扩展性和可维护性。本指南将带你深入了解如何使用 Spring Boot 构建专业级的 RESTful 服务。
            </p>
        </div>

        <!-- REST Architecture Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">RESTful 架构概览</h2>
            <div class="mermaid">
                graph TB
                    A[客户端] -->|HTTP 请求| B[RESTful API]
                    B --> C{HTTP 方法}
                    C -->|GET| D[获取资源]
                    C -->|POST| E[创建资源]
                    C -->|PUT| F[更新资源]
                    C -->|DELETE| G[删除资源]
                    B --> H[Controller 控制器]
                    H --> I[Service 服务层]
                    I --> J[Repository 数据层]
                    J --> K[(数据库)]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style K fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Core Concepts -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">
                <i class="fas fa-cube mr-3 feature-icon"></i>核心概念
            </h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="highlight-box">
                    <h3 class="text-xl font-semibold mb-3 text-purple-700">
                        <i class="fas fa-network-wired mr-2"></i>REST 原则
                    </h3>
                    <p class="text-gray-700">REST 是一种架构风格，通过 HTTP 协议提供资源的访问和操作。它强调无状态、统一接口、资源导向等原则。</p>
                </div>
                <div class="highlight-box">
                    <h3 class="text-xl font-semibold mb-3 text-purple-700">
                        <i class="fas fa-exchange-alt mr-2"></i>数据传输
                    </h3>
                    <p class="text-gray-700">RESTful API 通常使用 JSON 或 XML 格式传输数据，其中 JSON 因其轻量级和易读性成为首选。</p>
                </div>
            </div>
        </div>

        <!-- Implementation Steps -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">
                <i class="fas fa-tasks mr-3 feature-icon"></i>实现步骤
            </h2>
            
            <!-- Step 1 -->
            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <span class="bg-purple-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-3">1</span>
                    创建 Spring Boot 项目
                </h3>
                <p class="text-gray-700 mb-4">使用 Spring Boot 简化项目配置，添加必要的依赖：</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-code mr-2"></i>pom.xml
                    </div>
                    <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;</code></pre>
                </div>
            </div>

            <!-- Step 2 -->
            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <span class="bg-purple-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-3">2</span>
                    定义资源实体
                </h3>
                <p class="text-gray-700 mb-4">创建表示资源的实体类，例如图书管理系统中的 Book 类：</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-code mr-2"></i>Book.java
                    </div>
                    <pre><code>import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    
    // Getters and Setters
}</code></pre>
                </div>
            </div>

            <!-- Step 3 -->
            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <span class="bg-purple-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-3">3</span>
                    创建数据访问层
                </h3>
                <p class="text-gray-700 mb-4">使用 Spring Data JPA 简化数据库操作：</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-code mr-2"></i>BookRepository.java
                    </div>
                    <pre><code>import org.springframework.data.jpa.repository.JpaRepository;

public interface BookRepository extends JpaRepository&lt;Book, Long&gt; {
}</code></pre>
                </div>
            </div>

            <!-- Step 4 -->
            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <span class="bg-purple-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-3">4</span>
                    实现服务层
                </h3>
                <p class="text-gray-700 mb-4">服务层负责业务逻辑的实现：</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-code mr-2"></i>BookService.java
                    </div>
                    <pre><code>import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class BookService {
    @Autowired
    private BookRepository bookRepository;

    public List&lt;Book&gt; getAllBooks() {
        return bookRepository.findAll();
    }

    public Optional&lt;Book&gt; getBookById(Long id) {
        return bookRepository.findById(id);
    }

    public Book saveBook(Book book) {
        return bookRepository.save(book);
    }

    public void deleteBook(Long id) {
        bookRepository.deleteById(id);
    }
}</code></pre>
                </div>
            </div>

            <!-- Step 5 -->
            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 flex items-center">
                    <span class="bg-purple-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-3">5</span>
                    创建 RESTful 控制器
                </h3>
                <p class="text-gray-700 mb-4">控制器处理 HTTP 请求并返回响应：</p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-code mr-2"></i>BookController.java
                    </div>
                    <pre><code>import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping
    public List&lt;Book&gt; getAllBooks() {
        return bookService.getAllBooks();
    }

    @GetMapping("/{id}")
    public ResponseEntity&lt;Book&gt; getBookById(@PathVariable Long id) {
        Optional&lt;Book&gt;